package smdc.system.service.impl;

import java.util.Date;
import java.util.List;

import smdc.common.config.RuoYiConfig;
import smdc.common.core.domain.entity.SysUser;
import smdc.common.utils.SecurityUtils;
import smdc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import smdc.system.mapper.EasBaseCourseMapper;
import smdc.system.domain.EasBaseCourse;
import smdc.system.service.IEasBaseCourseService;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-18
 */
@Service
public class EasBaseCourseServiceImpl implements IEasBaseCourseService 
{
    @Autowired
    private EasBaseCourseMapper easBaseCourseMapper;

    /**
     * 查询订单信息
     * 
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public EasBaseCourse selectEasBaseCourseById(String id)
    {
        String filePath = RuoYiConfig.getUploadPath();
        EasBaseCourse easBaseCourse = easBaseCourseMapper.selectEasBaseCourseById(id);
//        if (StringUtils.isNotBlank(easBaseCourse.getAvatar())){
//            easBaseCourse.setAvatar(filePath + easBaseCourse.getAvatar());
//        }
        return easBaseCourse;
    }

    /**
     * 查询订单信息列表
     * 
     * @param easBaseCourse 订单信息
     * @return 订单信息
     */
    @Override
    public List<EasBaseCourse> selectEasBaseCourseList(EasBaseCourse easBaseCourse)
    {
        return easBaseCourseMapper.selectEasBaseCourseList(easBaseCourse);
    }

    /**
     * 新增订单信息
     * 
     * @param easBaseCourse 订单信息
     * @return 结果
     */
    @Override
    public int insertEasBaseCourse(EasBaseCourse easBaseCourse)
    {
        Date date = new Date(System.currentTimeMillis());
        easBaseCourse.setNowDate(date);
        SysUser user = SecurityUtils.getLoginUser().getUser();
        easBaseCourse.setCreateBy(user.getUserName());
        easBaseCourse.setUpdateBy(user.getUserName());
        easBaseCourse.setUpdateTime(date);
        easBaseCourse.setCreateTime(date);
//        easBaseCourse.setDeptId(user.getDeptId());
        return easBaseCourseMapper.insertEasBaseCourse(easBaseCourse);
    }

    /**
     * 修改订单信息
     * 
     * @param easBaseCourse 订单信息
     * @return 结果
     */
    @Override
    public int updateEasBaseCourse(EasBaseCourse easBaseCourse)
    {
        return easBaseCourseMapper.updateEasBaseCourse(easBaseCourse);
    }

    /**
     * 修改订单信息
     *
     * @param easBaseCourse 订单信息
     * @return 结果
     */
    @Override
    public int updateRateEasBaseCourse(EasBaseCourse easBaseCourse)
    {
        String[] ids = easBaseCourse.getIds();
//        String[] idArray;
//        if (ids.contains(",")) {
//            idArray = ids.split(",");
//        } else {
//            idArray = new String[]{ids};
//        }
        /**
         * 成本 订单价格*成本汇率
         * 预估返款 订单价格*返款汇率
         * 预估利润 订单价格-成本
         * 真实利润 返款金额-成本
         */
        for (String id : ids) {
            EasBaseCourse easBaseCourse1 = easBaseCourseMapper.selectEasBaseCourseById(id);

            String price = easBaseCourse1.getPrice();//订单价格
            String exchangeRate = easBaseCourse.getExchangeRate();//成本汇率
            String returnRate = easBaseCourse.getReturnRate();//返款汇率

            double price1 = Double.parseDouble(price);//订单价格
            double exchangeRate1 = Double.parseDouble(exchangeRate);//成本汇率
            double returnRate1 = Double.parseDouble(returnRate);//返款汇率
            double cost1 = price1 * exchangeRate1;//成本
            double estimatedCounterpayment1 = price1 * returnRate1;//预估返款
            double eestimatedProfit1 = estimatedCounterpayment1 - cost1;//预估利润

            if (easBaseCourse1.getRepayAmount() != null && !easBaseCourse1.getRepayAmount().isEmpty()){//返款金额不为空
                String repayAmount = easBaseCourse1.getRepayAmount();//返款金额
                double repayAmount1 = Double.parseDouble(repayAmount);//返款金额
                double realProfit1 = repayAmount1 - cost1; //真实利润
                easBaseCourse1.setEstimatedProfit(String.format("%.2f", realProfit1));//真实利润
            }

            easBaseCourse1.setExchangeRate(exchangeRate);//成本汇率
            easBaseCourse1.setReturnRate(returnRate);//返款汇率

            easBaseCourse1.setCost(String.format("%.2f", cost1));//成本
            easBaseCourse1.setEstimatedCounterpayment(String.format("%.2f", estimatedCounterpayment1));//预估返款
            easBaseCourse1.setEstimatedProfit(String.format("%.2f", eestimatedProfit1));//预估利润

            easBaseCourseMapper.updateEasBaseCourse(easBaseCourse1);
        }
        return 1;
    }

    /**
     * 批量删除订单信息
     * 
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteEasBaseCourseByIds(String[] ids)
    {
        return easBaseCourseMapper.deleteEasBaseCourseByIds(ids);
    }

    /**
     * 删除订单信息信息
     * 
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteEasBaseCourseById(String id)
    {
        return easBaseCourseMapper.deleteEasBaseCourseById(id);
    }
}
